package com.anivia.common.web.config.feign;

import com.anivia.common.core.constant.CommonStant;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * 配置Feign的请求拦截器，用于将token放入请求头
 */
@Configuration
public class FeignConfig implements RequestInterceptor {

    /**
     * 将请求token放入请求头
     */
    @Override
    public void apply(RequestTemplate requestTemplate) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (null != attributes) {
            HttpServletRequest request = attributes.getRequest();
            requestTemplate.header(CommonStant.AUTHORIZATION, request.getHeader(CommonStant.AUTHORIZATION));
        }
    }
}
